package com.example.project.service.impl;


import com.example.project.entity.Review;
import com.example.project.enums.ReviewStateEnum;
import com.example.project.mapper.ReviewMapper;
import com.example.project.service.ReviewService;
import com.example.project.utils.SecurityUtils;
import com.example.project.vo.model.CommonRequest;
import com.example.project.vo.model.DoReviewVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReviewServiceImpl extends ServiceImpl<ReviewMapper, Review> implements ReviewService {
    private static final String workTable = "apply";

    @Resource
    private ReviewMapper reviewMapper;

    @Override
    public PageInfo<Review> pageMyReview(CommonRequest request) {
        PageHelper.startPage(request.getPage(), request.getSize());

        Long reviewId = SecurityUtils.getUser().getId();
        LambdaQueryWrapper<Review> wrapper = new LambdaQueryWrapper<Review>()
                .eq(Review::getReviewId, reviewId);
        List<Review> reviewList = this.getBaseMapper().selectList(wrapper);
        return new PageInfo<>(reviewList);
    }

    @Override
    public void doReview(DoReviewVo doReviewVo) {

        // 修改审核状态表
        LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<Review>()
                .set(Review::getReviewReason, doReviewVo.getReason())
                .set(Review::getState, doReviewVo.getState())
                .set(Review::getReviewTime, LocalDateTime.now())
                .eq(Review::getReviewId, doReviewVo.getReviewId());
        this.getBaseMapper().update(null, updateWrapper);
        // 修改 任务表
        updateWorkState(doReviewVo.getWorkId(), doReviewVo.getState());

    }

    @Override
    public void reApply(Long reviewId, Long workId) {
        // 修改审核表
        LambdaUpdateWrapper<Review> updateWrapper = new LambdaUpdateWrapper<Review>()
                .set(Review::getState, ReviewStateEnum.UN_REVIEW)
                .set(Review::getReviewTime, null)
                .eq(Review::getId, reviewId);
        this.getBaseMapper().update(null, updateWrapper);

        // 修改任务表
        updateWorkState(workId, ReviewStateEnum.UN_REVIEW);
    }

    @Override
    public void annulReview(Long reviewId, Long workId) {
        // 修改审核表
        LambdaUpdateWrapper<Review> updateWrapper = new LambdaUpdateWrapper<Review>()
                .set(Review::getState, ReviewStateEnum.ANNUL_REVIEW)
                .set(Review::getReviewTime, null)
                .eq(Review::getWorkId, workId);
        this.getBaseMapper().update(null, updateWrapper);
        // 修改任务表
        updateWorkState(workId, ReviewStateEnum.ANNUL_REVIEW);

    }

    @Override
    public List<Review> process(Long workId) {
        LambdaQueryWrapper<Review> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Review::getWorkId, workId);
        return this.getBaseMapper().selectList(wrapper);
    }

    // 修改任务表
    private void updateWorkState(Long workId, ReviewStateEnum stateEnum) {
        int state = stateEnum.getId();
        reviewMapper.updateWorkState(workTable, workId, state);
    }
}
